import request from './request'

// 用户相关接口
export interface User {
  id: number
  username: string
  email: string
  avatar?: string
}

// 登录参数
export interface LoginParams {
  username: string
  password: string
}

// 登录响应
export interface LoginResponse {
  token: string
  user: User
}

// 用户API服务
export const userApi = {
  // 用户登录
  login: (data: LoginParams) =>
    request.post<LoginResponse>('/auth/login', data),

  // 获取用户信息
  getUserInfo: () =>
    request.get<User>('/user/info'),

  // 获取用户列表
  getUserList: (params?: { page?: number; size?: number }) =>
    request.get<{ list: User[]; total: number }>('/users', { params }),

  // 更新用户信息
  updateUser: (id: number, data: Partial<User>) =>
    request.put<User>(`/users/${id}`, data),

  // 删除用户
  deleteUser: (id: number) =>
    request.delete(`/users/${id}`),

  // 上传头像
  uploadAvatar: (file: File) => {
    const formData = new FormData()
    formData.append('avatar', file)
    return request.upload<{ url: string }>('/upload/avatar', formData)
  }
}

// 示例：其他业务模块的API
export const postApi = {
  // 获取文章列表
  getPosts: (params?: { page?: number; category?: string }) =>
    request.get('/posts', { params }),

  // 获取文章详情
  getPost: (id: number) =>
    request.get(`/posts/${id}`),

  // 创建文章
  createPost: (data: { title: string; content: string; category: string }) =>
    request.post('/posts', data),

  // 更新文章
  updatePost: (id: number, data: Partial<{ title: string; content: string; category: string }>) =>
    request.put(`/posts/${id}`, data),

  // 删除文章
  deletePost: (id: number) =>
    request.delete(`/posts/${id}`)
}

// 导出默认的 request 实例，供其他地方直接使用
export { default as request } from './request'
